package com.example.shop.fragment;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shop.R;
import com.example.shop.adapter.ItemExlvSearchFragAdapter;
import com.example.shop.base.SCBaseFragment;
import com.example.shop.db.dao.SearchHistoryDao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/3/11.
 */
public class SearchFragment extends SCBaseFragment implements View.OnClickListener, ExpandableListView.OnGroupExpandListener, ExpandableListView.OnGroupCollapseListener {

    @Bind(R.id.edt_search_frag)
    EditText mEdtSearchFrag;
    @Bind(R.id.exlv_search_frag)
    ExpandableListView mExpandableListView;
    private String[] mGroupTitles;
    private String[] mRMSSTitles;
    private String[] mLSJLTitles;
    List<String[]> mChildDatas = new ArrayList<>();
    private ItemExlvSearchFragAdapter mItemExlvSearchFragAdapter;
    private SearchHistoryDao mSearchHistoryDao;
    private Set<Integer> expandItem=new HashSet<>();

    @Override
    public View setContentView(ViewGroup container) {
        View conentView = mLayoutInflater.inflate(R.layout.fragment_search, container, false);
        setTitleCenter("搜索界面");
        setTvTitleBarRight("搜索", this);
        ButterKnife.bind(this, conentView);
        initDatas();
        initEdt();
        initExlv();

        return conentView;
    }

    private void initEdt() {
        mEdtSearchFrag.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    Toast.makeText(mBaseActivity, "ok", Toast.LENGTH_SHORT).show();
                    resetHistory();
                    System.out.println("调用了几次？？？");
                return true;
            }
        });
    }

    private void initExlv() {
        mItemExlvSearchFragAdapter = new ItemExlvSearchFragAdapter(mBaseActivity, mChildDatas, mGroupTitles);
        mExpandableListView.setAdapter(mItemExlvSearchFragAdapter);
        hfzt();
        mExpandableListView.setOnGroupExpandListener(this);
        mExpandableListView.setOnGroupCollapseListener(this);
    }

    private void hfzt() {
        for (int pos:expandItem){
            mExpandableListView.expandGroup(pos);
        }
    }

    private void initDatas() {
        mChildDatas.clear();
        mGroupTitles = mBaseActivity.getResources().getStringArray(R.array.searchtitles);
        mRMSSTitles = mBaseActivity.getResources().getStringArray(R.array.rmss);
        if (mSearchHistoryDao == null) {
            mSearchHistoryDao = new SearchHistoryDao();
        }
        mLSJLTitles = mSearchHistoryDao.getAllItems();
        mChildDatas.add(mRMSSTitles);
        mChildDatas.add(mLSJLTitles);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    //添加历史记录
    public void resetHistory(){
        String searchTitle = mEdtSearchFrag.getText().toString();
        if (TextUtils.isEmpty(searchTitle)) {
            Toast.makeText(mBaseActivity, "搜索内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        mSearchHistoryDao.addItem(searchTitle);

        //从新获取全部数据，重新设置适配器
        initDatas();
        initExlv();
    }
    @Override
    public void onClick(View v) {
        resetHistory();
    }

    @Override
    public void onGroupExpand(int groupPosition) {
        expandItem.add(groupPosition);
    }

    @Override
    public void onGroupCollapse(int groupPosition) {
        expandItem.remove((Integer)groupPosition);
    }
}
